﻿Public Class associations1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PanelCreateAsso.Visible = False
        If Request.QueryString("id_asso") <> "" Then
            PanelListAsso.Visible = False
            PanelEditAsso.Visible = True
        Else
            PanelListAsso.Visible = True
            PanelEditAsso.Visible = False
        End If


        Dim svc As New IsisAssoWebServices.WebServicesClient
        Dim ReturnDataAsso = svc.AssoListOfUsers(My.Settings.pvkey, Session.Contents("id_user"))
        GridViewListAsso.DataSource = ReturnDataAsso
        GridViewListAsso.DataBind()
    End Sub

    Protected Sub ButtonCreateAsso_Click(sender As Object, e As EventArgs) Handles ButtonCreateAsso.Click
        PanelCreateAsso.Visible = True
        PanelListAsso.Visible = False
        Dim svc As New IsisAssoWebServices.WebServicesClient
        Dim ListActivite = svc.ActivitesList(My.Settings.pvkey)
        activites.DataSource = ListActivite
        activites.DataTextField = "nom_activite"
        activites.DataValueField = "id_activite"
        activites.DataBind()


    End Sub

    Protected Sub ButtonSubmit_Click(sender As Object, e As EventArgs) Handles ButtonSubmit.Click
        Dim svc As New IsisAssoWebServices.WebServicesClient
        Dim ReturnData As Long = svc.AssoCreate(My.Settings.pvkey, nom_asso.Text, adresse_l1.Text, adresse_l2.Text, adresse_l3.Text, adresse_cp.Text, adresse_ville.Text, telephone.Text, telecopie.Text, email.Text, activites.SelectedValue)
        PanelCreateAsso.Visible = False
        PanelListAsso.Visible = False
        PanelErrorCreateAsso.Visible = False
        PanelFormConfirm.Visible = False
        Select Case ReturnData
            '0 : no init
            '-1 : account with this email exist
            '-2 : error processing creation
            'else : account ok with ID returning

            Case 0
                MsgError.Text = "Erreur du Serveur (000), veuillez recommencer"
                PanelErrorCreateAsso.Visible = True
            Case -1
                MsgError.Text = "Cette association existe déjà dans cette ville, rapprochez vous du créateur eventuel ou recommencer."
                PanelErrorCreateAsso.Visible = True
            Case -2
                MsgError.Text = "Erreur du Serveur (002), veuillez recommencer"
                PanelErrorCreateAsso.Visible = True
            Case Else

                'creation des droits de cette association sur cet utilisateur

                Try
                    Dim ReturnData2 As Long = svc.UserRightCreate(My.Settings.pvkey, ReturnData, Session.Contents("id_user"), True, True, True, True)
                    If ReturnData2 = 0 Or ReturnData2 = -2 Then
                        svc.AssoDelete(My.Settings.pvkey, ReturnData)
                    End If
                    PanelFormConfirm.Visible = True
                Catch ex As Exception
                    'delete asso
                    svc.AssoDelete(My.Settings.pvkey, ReturnData)
                    MsgError.Text = "Erreur du Serveur (002), veuillez recommencer"
                    PanelErrorCreateAsso.Visible = True
                End Try

        End Select
    End Sub

    Protected Sub ButtonTryAgain_Click(sender As Object, e As EventArgs) Handles ButtonTryAgain.Click
        PanelListAsso.Visible = False
        PanelCreateAsso.Visible = True
        PanelErrorCreateAsso.Visible = False
        PanelFormConfirm.Visible = False
    End Sub


    Public Function GetAssociationDetail(id_asso As String) As String
        Dim ReturnData As String = ""
        Dim svc As New IsisAssoWebServices.WebServicesClient
        If IsNumeric(id_asso) Then


            Dim ReturnDataSvc = svc.AssoDetail(My.Settings.pvkey, CInt(id_asso))
            If ReturnDataSvc IsNot Nothing Then
                ReturnData += "<p id=""detail_asso""><b>" & ReturnDataSvc.nom_asso & "</b>"
                ReturnData += "<br />" & ReturnDataSvc.adresse_l1
                ReturnData += "<br />" & ReturnDataSvc.adresse_l2
                ReturnData += "<br />" & ReturnDataSvc.adresse_l3
                ReturnData += "<br />" & ReturnDataSvc.adresse_cp & " " & ReturnDataSvc.adresse_ville
                ReturnData += "<br /> Email : " & ReturnDataSvc.email
                ReturnData += "<br />Téléphone : " & ReturnDataSvc.telephone
                ReturnData += "<br />Télécopie : " & ReturnDataSvc.telecopie
                ReturnData += "</p>"
            End If

        End If


        Return ReturnData
    End Function

End Class